package org.jeecg.modules.water.controller;


import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import net.sf.json.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.water.entity.DataItem;
import org.jeecg.modules.water.entity.LwgyJointWater;
import org.jeecg.modules.water.service.ILwgyJointWaterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 轻松标记接收推送信息（nb表）
 *
 * */
@Log
@Api(tags = "轻松标记nb表")
@RestController
@RequestMapping("/nbApi")

public class LwgyWaterConnectController {

    @Autowired
    public ILwgyJointWaterService iLwgyJointWaterService;


    /**
     * 轻松系统调用第三方系统接口，推送抄表信息。
     * */
    @AutoLog(value = "轻松nb表-接收抄表信息")
    @ApiOperation(value = "轻松nb表-接收抄表信息", notes = "轻松nb表-接收抄表信息")
    @PostMapping("/postMeterReading")
    public Result<?> postMeterReading(@RequestBody DataItem[] dataItems) {

        log.info("收到一条抄表信息："+dataItems);

        List<LwgyJointWater> list = new ArrayList();
        for (DataItem item : dataItems) {
            System.out.println("Current Time: " + item.getCurtime());
            System.out.println("Counter's Curnum: " + item.getCounter().getCurnum());
            // 可以继续访问其他属性
            LwgyJointWater water = new LwgyJointWater();
            water.setIspid(item.getIspid());
            water.setCurtime(item.getCurtime());
            //water.setStationcode(object.stationcode);
         /*   System.out.println("ispid: " + object.ispid);
            System.out.println("curtime: " + object.curtime);
            //System.out.println("stationcode: " + object.stationcode);
            System.out.println("conter curnum: " + object.conter.curnum);*/

            water.setAlarmNum(item.getCounter().getAlarmNum());//alarmNum
            water.setAlarmTime(item.getCounter().getAlarmTime());//alarmTime
            water.setCurnum(item.getCounter().getCurnum());//curnum
            water.setValvestate(item.getCounter().getValvestate());//readstate
            water.setReadperiod(item.getCounter().getReadperiod());//readperiod
            water.setBattery(item.getCounter().getBattery());//battery
            water.setSensor(item.getCounter().getSensor());//sensor
            water.setSignalstrength(item.getCounter().getSignal());//signal
            water.setReadstate(item.getCounter().getReadstate());//valvestate
            // water.setReversenum(object.conter.reversenum);
            water.setMeterReadingType("0");
            System.out.println("conter curnum: " + item.getCounter().getCurnum());
            list.add(water);
        }

/*

            ObjectMapper objectMapper = new ObjectMapper();
            log.info("=====================1");
            MyObject[] objects = objectMapper.readValue(json, MyObject[].class);
            log.info("=====================2");
            List<LwgyJointWater> list = new ArrayList();
            log.info("=====================3");
            for (MyObject object : objects) {
                LwgyJointWater water = new LwgyJointWater();
                water.setIspid(object.ispid);
                water.setCurtime(object.curtime);
                //water.setStationcode(object.stationcode);
                System.out.println("ispid: " + object.ispid);
                System.out.println("curtime: " + object.curtime);
                //System.out.println("stationcode: " + object.stationcode);
                System.out.println("conter curnum: " + object.conter.curnum);

                water.setAlarmNum(object.conter.alarmNum);//alarmNum
                water.setAlarmTime(object.conter.alarmTime);//alarmTime
                water.setCurnum(object.conter.curnum);//curnum
                water.setValvestate(object.conter.valvestate);//readstate
                water.setReadperiod(object.conter.readperiod);//readperiod
                water.setBattery(object.conter.battery);//battery
                water.setSensor(object.conter.sensor);//sensor
                water.setSignalstrength(object.conter.signal);//signal
                water.setReadstate(object.conter.readstate);//valvestate
               // water.setReversenum(object.conter.reversenum);
                water.setMeterReadingType("0");
                System.out.println("conter curnum: " + object.conter.reversenum);
                list.add(water);
            }
            log.info("=====================4");*/
        iLwgyJointWaterService.saveBatch(list);

        return Result.OK("success");

    }


    static class MyObject {
            public String ispid;//表具通讯码
            public String curtime;//抄表时间
            public String stationcode;//分站号
            public Conter conter;
        }

    static   class Conter {
             public String alarmNum;//强磁攻击次数
             public String alarmTime;//强磁攻击时长
             public String battery;//电池状态
             public String curnum;//阀门状态
             public String valvestate;//阀门状态
             public String readperiod;//抄表周期
             public String sensor;//传感器状态
             public String signal;//信号强度值
             public String readstate;//校验是否有误
             public String reversenum;//反向读数(这个好像没有了 )
        }

}
